﻿using BDShop.DAL;
using System;
using System.Collections.Generic;
using IsolationLevel = System.Transactions.IsolationLevel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BDShop.Entities;

namespace BDShop.Business
{
    public class OrderService
    {
        public static long Insert(Order order)
        {
            try
            {
                using (var context = new BDShopEntities(IsolationLevel.ReadCommitted))
                {
                    context.Orders.Add(order);

                    context.SaveChanges();
                    return order.ID;
                }
            }
            catch (Exception)
            {
                return -1;
            }
           
        }

        public static List<Order> GetByUser(int userID)
        {
            using (var context = new BDShopEntities(IsolationLevel.ReadCommitted))
            {
               return context.Orders.Where(o=>o.CustomerID==userID).OrderByDescending(o=>o.DateCreated).ToList();
            }
        }

        public static Order GetByID(long id)
        {
            using (var context = new BDShopEntities(IsolationLevel.ReadCommitted))
            {
                return context.Orders.Where(o=>o.ID==id).FirstOrDefault();
            }
        }
    }
}
